// ActionScript file
package com.kinderas.data{
	public class ArrayExtended extends Array{
		/**
		 * Takes two indexes and swap their objects in the passed Array.
		 * To swap two object based on their content, use swapElementsByContent
		 * @param index1
		 * @param index2
		 * @param theArray
		 * @return 
		 * 
		 */		
		public static function swapElementsByIndex(index1:int, index2:int, theArray:Array):Array{
			var buffer:Object;
			if((index1 > -1 && index1 < theArray.length) && (index2 > -1 && index2 < theArray.length)){
				buffer = theArray[index1];
				theArray[index1] = theArray[index2];
				theArray[index2] = buffer;
				return theArray;
			}
			return theArray;
		}
		/**
		 * Takes two object to swap and swaps their place in the passed Array, based on their content.
		 * To swap elements in an Array containing several identical elements, 
		 * use the swapElementsByIndex function.
		 * <strong>Note:The function should only be used arrays having unique elements!</strong>
		 * @param item1
		 * @param item2
		 * @param theArray
		 * @return 
		 * 
		 */		
		public static function swapElementsByContent(item1:Object, item2:Object, theArray:Array):Array{
			var elem1:int;
			var elem2:int;
			var buffer:Object;
			if(inArray(item1,theArray) && inArray(item2,theArray)){
				for(var i:int=0;i<theArray.length;i++){
					if(theArray[i] == item1){
						elem1 = i;
					}
					if(theArray[i] == item2){
						elem2 = i;
					}
				}
				buffer = theArray[elem1];
				theArray[elem1] = theArray[elem2];
				theArray[elem2] = buffer;
				return theArray;
			}
			return theArray;
		}
		/**
		 * Takes a param of type Object and deletes it from the specified Array(second param)
		 * Returns an Array without the items specified.
		 * <strong>This function will delete ALL elements matching the searchparam from the Array</strong>
		 * To delete one spesific item from a non unique Array, use deleteOneItemByContent
		 * @param item
		 * @return 
		 * 
		 */			
		public static function deleteItemsByContent(itemToDelete:Object, theArray:Array):Array{
			var returnArray:Array = new Array();
			for each(var element:Object in theArray){
				if(element == itemToDelete){
					continue;
				}
				returnArray.push(element);
			}
			return returnArray;
		}
		/**
		 * Takes a param of type object and deletes the <i>first instance</i> of this item from the given Array(second param).
		 * Returns an Array without the item specified.
		 * To delete all instances of an item in an Array, use deleteItemsByContent
		 * @param itemToDelete
		 * @param theArray
		 * @return 
		 * 
		 */		
		public static function deleteOneItemByContent(itemToDelete:Object, theArray:Array):Array{
			var ind:uint = theArray.indexOf(itemToDelete);
			theArray.splice(ind,1);
			return theArray;
		}
		/**
		 * Takes a index and an Array. Removes the element denoted by the index
		 * and returns an Array without the element at the position of the index
		 * @param index
		 * @param theArray
		 * @return 
		 * 
		 */		
		public static function deleteItemByIndex(index:int, theArray:Array):Array{
			theArray.splice(index,1);
			return theArray;
		}
		/**
		 * Takes an object to find and an Array. return true if element is found in the Array, 
		 * false if not
		 * @param item
		 * @param theArray
		 * @return 
		 * 
		 */		
		public static function inArray(item:Object, theArray:Array):Boolean{
			for each(var element:Object in theArray){
				if(element == item){
					return true;
				}
			}
			return false;
		}
		 /**
		  * Takes an Array and radomize it's members returning a 
		  * ramdomized Array leaving the original Array intact 
		  * @param whichArray
		  * @return 
		  * 
		  */		
		 public static function shuffle(whichArray:Array):Array {
			var oldArray:Array = new Array();
			for each(var obj:Object in whichArray){
				oldArray.push(obj);
			}
			var newArray:Array = new Array();
			while (oldArray.length>0) {
				newArray.push(oldArray.splice(Math.floor((oldArray.length)*Math.random()), 1)[0]);
			}
			oldArray = newArray;
			return (newArray);
		}
	}
}